package com.ioc.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
@Aspect
public class MethodAspect {
    @Around("execution(public * com.ioc..*.*(..))")
    public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
        try {
            long start = new Date().getTime();
            Object proceed = joinPoint.proceed();
            long end = new Date().getTime();
            long duration = end - start;
            if (duration >= 1000) {
                String className = joinPoint.getTarget().getClass().getName();
                String methodName = joinPoint.getSignature().getName();
                System.out.println("-------->" + className + "." + methodName + "方法执行时间：" + duration + "ms");
            }
            return proceed;
        } catch (Throwable throwable) {
            throw throwable;
        }
    }
}
